#import "PAFilenameListInspector.h"
#import <appkit/appkit.h>
#import "PAFilenameList.h"

@implementation PAFilenameListInspector

- init
{
    char buf[MAXPATHLEN + 1];
    id bundle;
    
    [super init];
    
    bundle = [NXBundle bundleForClass:[PAStringList class]];
    [bundle getPath:buf forResource:"PAFilenameListInspector" ofType:"nib"];
    [NXApp loadNibFile:buf owner:self withNames:NO fromZone:[self zone]];
	[stringText setDelegate:self];
    return self;
}

- textDidGetKeys:sender isEmpty:(BOOL)flag 
{ [self perform:@selector(ok:) with:self afterDelay:500 cancelPrevious:YES]; return self; }

- (BOOL)wantsButtons { return NO; }

- revert:sender
{
	int i;
	NXStream *s = NXOpenMemory(NULL, 0, NX_READWRITE);
	
	// Add strings to text object
	for(i=0; i<[object count]; i++) NXPrintf(s,"%s\n", [object stringAt:i]);
	NXSeek(s, 0, NX_FROMSTART);
	[stringText readText:s];
	NXCloseMemory(s, NX_FREEBUFFER);
		
	// Get the preload libraries, directories and filenames
	[libraryText setStringValue:[object preLoadLibraries]];
	[directoryText setStringValue:[object preLoadDirectories]];
	[plFromAppDirSwitch setState:[object preLoadFromAppDir]];
	[plFromBundleDirSwitch setState:[object preLoadFromBundleDir]];
	[extensionText setStringValue:[object preLoadExtensions]];
	[stripPathSwitch setState:[object preLoadStripPath]];
	[stripExtSwitch setState:[object preLoadStripExt]];
	[ifAbsentSwitch setState:[object preLoadIfAbsent]];
	[sortedSwitch setState:[object preLoadSorted]];
	
	return [super revert:sender];
}

- ok:sender
{
	char *buffer;

	[object empty];

	// Add all of the strings from text object
	buffer = malloc([stringText textLength]*(sizeof(char)+1));
	[stringText getSubstring:buffer start:0 length:[stringText textLength]+ 1];
	[object addDelimitedFilenames:buffer delimiters:"\n" stripPath:NO 
		stripExt:NO ifAbsent:[ifAbsentSwitch state] sorted:[sortedSwitch state] 
		at:[object count]];	
	free(buffer);

	// Set preload libraries, directories and extensions
	[object setPreLoadLibraries:[libraryText stringValue]];
	[object setPreLoadDirectories:[directoryText stringValue]];
	[object setPreLoadFromAppDir:[plFromAppDirSwitch state]];
	[object setPreLoadFromBundleDir:[plFromBundleDirSwitch state]];
	[object setPreLoadExtensions:[extensionText stringValue]];
	[object setPreLoadStripPath:[stripPathSwitch state]];
	[object setPreLoadStripExt:[stripExtSwitch state]];
	[object setPreLoadIfAbsent:[ifAbsentSwitch state]];
	[object setPreLoadSorted:[sortedSwitch state]];
	
	return [super ok:sender];
}

@end
